{
 "cells": [
  {
   "cell_type": "markdown",
   "id": "google",
   "metadata": {},
   "source": [
    "##### Copyright 2025 Google LLC."
   ]
  },
  {
   "cell_type": "markdown",
   "id": "apache",
   "metadata": {},
   "source": [
    "Licensed under the Apache License, Version 2.0 (the \"License\");\n",
    "you may not use this file except in compliance with the License.\n",
    "You may obtain a copy of the License at\n",
    "\n",
    "    http://www.apache.org/licenses/LICENSE-2.0\n",
    "\n",
    "Unless required by applicable law or agreed to in writing, software\n",
    "distributed under the License is distributed on an \"AS IS\" BASIS,\n",
    "WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.\n",
    "See the License for the specific language governing permissions and\n",
    "limitations under the License.\n"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "basename",
   "metadata": {},
   "source": [
    "# simple_min_cost_flow_program"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "link",
   "metadata": {},
   "source": [
    "<table align=\"left\">\n",
    "<td>\n",
    "<a href=\"https://colab.research.google.com/github/google/or-tools/blob/main/examples/notebook/graph/simple_min_cost_flow_program.ipynb\"><img src=\"https://raw.githubusercontent.com/google/or-tools/main/tools/colab_32px.png\"/>Run in Google Colab</a>\n",
    "</td>\n",
    "<td>\n",
    "<a href=\"https://github.com/google/or-tools/blob/main/ortools/graph/samples/simple_min_cost_flow_program.py\"><img src=\"https://raw.githubusercontent.com/google/or-tools/main/tools/github_32px.png\"/>View source on GitHub</a>\n",
    "</td>\n",
    "</table>"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "doc",
   "metadata": {},
   "source": [
    "First, you must install [ortools](https://pypi.org/project/ortools/) package in this colab."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "install",
   "metadata": {},
   "outputs": [],
   "source": [
    "%pip install ortools"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "description",
   "metadata": {},
   "source": [
    "\n",
    "From Bradley, Hax and Maganti, 'Applied Mathematical Programming', figure 8.1."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "code",
   "metadata": {},
   "outputs": [],
   "source": [
    "import numpy as np\n",
    "\n",
    "from ortools.graph.python import min_cost_flow\n",
    "\n",
    "\n",
    "def main():\n",
    "    \"\"\"MinCostFlow simple interface example.\"\"\"\n",
    "    # Instantiate a SimpleMinCostFlow solver.\n",
    "    smcf = min_cost_flow.SimpleMinCostFlow()\n",
    "\n",
    "    # Define four parallel arrays: sources, destinations, capacities,\n",
    "    # and unit costs between each pair. For instance, the arc from node 0\n",
    "    # to node 1 has a capacity of 15.\n",
    "    start_nodes = np.array([0, 0, 1, 1, 1, 2, 2, 3, 4])\n",
    "    end_nodes = np.array([1, 2, 2, 3, 4, 3, 4, 4, 2])\n",
    "    capacities = np.array([15, 8, 20, 4, 10, 15, 4, 20, 5])\n",
    "    unit_costs = np.array([4, 4, 2, 2, 6, 1, 3, 2, 3])\n",
    "\n",
    "    # Define an array of supplies at each node.\n",
    "    supplies = [20, 0, 0, -5, -15]\n",
    "\n",
    "    # Add arcs, capacities and costs in bulk using numpy.\n",
    "    all_arcs = smcf.add_arcs_with_capacity_and_unit_cost(\n",
    "        start_nodes, end_nodes, capacities, unit_costs\n",
    "    )\n",
    "\n",
    "    # Add supply for each nodes.\n",
    "    smcf.set_nodes_supplies(np.arange(0, len(supplies)), supplies)\n",
    "\n",
    "    # Find the min cost flow.\n",
    "    status = smcf.solve()\n",
    "\n",
    "    if status != smcf.OPTIMAL:\n",
    "        print(\"There was an issue with the min cost flow input.\")\n",
    "        print(f\"Status: {status}\")\n",
    "        exit(1)\n",
    "    print(f\"Minimum cost: {smcf.optimal_cost()}\")\n",
    "    print(\"\")\n",
    "    print(\" Arc    Flow / Capacity Cost\")\n",
    "    solution_flows = smcf.flows(all_arcs)\n",
    "    costs = solution_flows * unit_costs\n",
    "    for arc, flow, cost in zip(all_arcs, solution_flows, costs):\n",
    "        print(\n",
    "            f\"{smcf.tail(arc):1} -> \"\n",
    "            f\"{smcf.head(arc)}  {flow:3}  / {smcf.capacity(arc):3}       {cost}\"\n",
    "        )\n",
    "\n",
    "\n",
    "main()\n",
    "\n"
   ]
  }
 ],
 "metadata": {
  "language_info": {
   "name": "python"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 5
}
